package com.scm.endpoint;

import java.util.List;

import org.apache.log4j.Logger;

import scm.classifica.jaxb.CLSBean;
import scm.classifica.jaxb.Team;
import scm.live.jaxb.CMPBean;
import scm.live.jaxb.MATCHTBean;
import scm.quote.jaxb.Match;
import scm.quote.jaxb.SCMBean;

import com.scm.ingestion.Ingestion;
import com.scm.ingestion.IngestionImpl;
import com.scm.integration.InterfaceSCM;
import com.scm.integration.SCMImpl;
import com.scm.property.IPropertyConfig;
import com.scm.property.IPropertyIDCamp;
import com.scm.property.PropertyConfig;
import com.scm.property.PropertyIDCamp;

public class ServiceImpl implements IService {
	
	private static Logger log = Logger.getLogger(ServiceImpl.class);
	
	private Ingestion ingestion =  null;
	private IPropertyConfig prop = null;
	private IPropertyIDCamp legenda =  null;
	private InterfaceSCM intf =  null;
	
	public ServiceImpl() {
		 ingestion = new IngestionImpl();
		 prop = new PropertyConfig();
		 legenda = new PropertyIDCamp();
		 intf = new SCMImpl();
	}
	
	
	public void ingestionQuote() {
		ingestion.startDeleteQuote();
		List<String> listCampIngestion = prop.getListaCmpIngestion();
		SCMBean scm = intf.getQuote();
		log.info("Ingestion Quote - Campionati "+listCampIngestion.size());
			if(scm != null)
				for(Match match : scm.getMatch()){
					if(match != null){
						if(listCampIngestion.contains(match.getTipoAvv())){
							ingestion.startIngestionQuote(match);
						}
					}
			}
	     log.info("Ingestion Quote Finished!");
	}


	public void ingestionClassifiche() {
		 ingestion.startDeleteClass();
		 List<String> listCampIngestion = prop.getListaCmpIngestion();
		 for(String ID : listCampIngestion){
			    log.info("Ingestion Classifica "+legenda.getStringName(ID));
				CLSBean resultClss = intf.getClassifica(legenda.getStringName(ID));
				if(resultClss != null)
					for(Team team : resultClss.getTeam()){
						ingestion.startIngestionClass(team);
					}
			}
		 log.info("Ingestion Classifica Finished!");
		
	}


	public void ingestionLive() {
		ingestion.startDeleteLive();
		ingestion.startDeleteCmpLive();
		log.info("Ingestion Live");
		for(CMPBean compList : intf.getCmpLive()){
			log.info("Ingestion Live Competizione:"+ compList.getNAMECOMPETATION());
			ingestion.startIngestionCompetation(compList);
			for(MATCHTBean  match : intf.getLiveScore(compList)){
				ingestion.startIngestionLive(match);
			}
		}
		log.info("Ingestion Live Finished!");
	}


	public void ingestionCampionati() {
		ingestion.startDeleteIdCmp();
		List<String> idListCampionati = intf.getIdCampionati();	
		List<String> listCampIngestion = prop.getListaCmpIngestion();
		
		for(String id : idListCampionati){
			if(listCampIngestion.contains(id)){
		    	ingestion.startIngestionIDCamp(id,listCampIngestion.indexOf(id));
			}
		}
	}

}
